package com.mlethe.android.demo.recyclerview.adapter;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.mlethe.android.demo.recyclerview.adapter.adapter.ExpandableItemAdapter;
import com.mlethe.android.demo.recyclerview.adapter.entity.Dog;
import com.mlethe.android.demo.recyclerview.adapter.entity.Level0Item;
import com.mlethe.android.demo.recyclerview.adapter.entity.Level1Item;
import com.mlethe.android.demo.recyclerview.adapter.entity.Level2Item;
import com.mlethe.demo.recyclerview.adapter.R;

import java.util.ArrayList;
import java.util.Random;

public class ExpandableUseActivity extends AppCompatActivity {

    private ExpandableItemAdapter mAdapter = new ExpandableItemAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_use);
        findViewById(R.id.btn_collapse_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.collapseAll(true);
            }
        });

        findViewById(R.id.btn_expand_all).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.expandAll(true);
            }
        });
        ((CheckBox) findViewById(R.id.cbOnlyExpandOne)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mAdapter.setOnlyExpandOne(isChecked);
            }
        });
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
        final GridLayoutManager manager = new GridLayoutManager(this, 3);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                Object item = mAdapter.getItem(position);
                if (item instanceof Dog) {
                    return 1;
                }
                return manager.getSpanCount();
            }
        });
        mAdapter.setData(generateData());
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(mAdapter);
        mAdapter.expandAll();
    }

    private ArrayList<Object> generateData() {
        int lv0Count = 9;
        int lv1Count = 3;
        int lv2Count = 4;
        int personCount = 5;

        String[] nameList = {"Bob", "Andy", "Lily", "Brown", "Bruce"};
        Random random = new Random();

        ArrayList<Object> res = new ArrayList<>();
        for (int i = 0; i < lv0Count; i++) {
            Level0Item lv0 = new Level0Item("This is " + i + "th item in Level 0", "subtitle of " + i);
            for (int j = 0; j < lv1Count; j++) {
                Level1Item lv1 = new Level1Item("Level 1 item: " + j, "(no animation)");
                for (int l = 0; l < lv2Count; l++) {
                    Level2Item lv2 = new Level2Item("Level 2 item: " + l, "(no animation)");
                    for (int k = 0; k < personCount; k++) {
                        lv2.addSubItem(new Dog(nameList[k], random.nextInt(20)));
                    }
                    lv1.addSubItem(lv2);
                }
                lv0.addSubItem(lv1);
            }
            res.add(lv0);
        }

        return res;
    }
}